package config;

import config.Interceptor.SessionInterceptor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: liujh
 * @Date:
 * @UpdateUser: liujh
 * @Date: 2020/09/29$ 08:51$
 * @UpdateRemark:
 * @Version: 1.0
 */
@Configuration
//@Profile({"prod","dev"})  //只有开发和生产环境，拦截器才启作用
@ConfigurationProperties(prefix = "config.path")  //读取配置文件
@Data
public class InterceptorConfig implements WebMvcConfigurer {



    private List<String> normal = new ArrayList<>();

    private List<String> special = new ArrayList<>();

    private List<String> exclude = new ArrayList<>();

    @Autowired
    StringRedisTemplate  redisTemplate;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(authenticationInterceptor())
//                .addPathPatterns("/**");    // 拦截所有请求，通过判断是否有 @LoginRequired 注解 决定是否需要登录
        registry.addInterceptor(new SessionInterceptor(redisTemplate))
                .excludePathPatterns(exclude)   //  不拦截
                .addPathPatterns(special); //  拦截

    }

   /* @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }*/



}
